//
//  ViewController.m
//  ButtonUse
//
//  Created by liuchengming on 16/7/27.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIButton * btn ;
}

@end

@implementation ViewController

//创建按钮  以及事件
- (void) createBtn
{
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    btn.frame = CGRectMake(100, 100, 80, 40);
   
    btn.backgroundColor = [UIColor cyanColor];
    
    [btn setTitle:@"改变颜色" forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(onCilt) forControlEvents:UIControlEventTouchDown];
    
    UIButton * exit = [UIButton buttonWithType:UIButtonTypeSystem];
    
    exit.frame = CGRectMake(270, 500, 50, 30);
    
    [exit setTitle:@"退出" forState:UIControlStateNormal];
    
    [exit addTarget:self action:@selector(onClir) forControlEvents:UIControlEventTouchUpInside];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:btn];
    [self.view addSubview:exit];
}

//变颜色按钮事件
- (void)onCilt
{
    
    if(self.view.backgroundColor == [UIColor purpleColor])
    {
        self.view.backgroundColor = [UIColor greenColor];
    
    //[btn addTarget:self action:@selector(onClir) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor yellowColor];
    }else if(self.view.backgroundColor == [UIColor greenColor])
    {
        self.view.backgroundColor = [UIColor grayColor];
        
        //[btn addTarget:self action:@selector(onClir) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor redColor];
    }else
    {
        self.view.backgroundColor = [UIColor purpleColor];
        
        //[btn addTarget:self action:@selector(onClir) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor blueColor];
    }
    
    
}

//退出按钮事件
- (void)onClir
{
    exit(0);
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
